#include "SeqStack.h"

//1，初始化栈
SeqStack* Init_SeqStack() {
    SeqStack* stack = (SeqStack*)malloc(sizeof(SeqStack));
    for(int i = 0;i < MAX_SIZE;i++) {
        stack->data[i] = NULL;
    }

    stack->size = 0;

    return stack;
}
//2，入栈
void Push_SeqStack(SeqStack* stack,void* data) {
    if(stack == NULL) return;
    if(data == NULL) return;
    if(stack->size == MAX_SIZE) return;

    stack->data[stack->size] = data;
    stack->size++;

}
//3，返回栈顶元素
void* Top_SeqStack(SeqStack* stack) {
    if(stack == NULL) return NULL;
    if(stack->size == 0) return NULL;

    return stack->data[stack->size - 1];
}
//4，出栈
void* Pop_SeqStack(SeqStack* stack) {
    if(stack == NULL) return NULL;
    if(stack->size == 0) return NULL;

    void* save = stack->data[stack->size - 1];

    stack->data[stack->size - 1] = NULL;
    stack->size--;
    return save;
}
//5，判断是否为空
int IsEmpty(SeqStack* stack) {
    if(stack == NULL) return -1;
    if(stack->size == 0) return 1;
    return 0;
}
//6，返回栈中元素的个数
int Size_SeqStack(SeqStack* stack) {
    if(stack == NULL) return -1;
    return stack->size;
}
//7，销毁
void FreeSpace_SeqStack(SeqStack* stack) {
    if(stack == NULL) return;

    free(stack);
}
//8，清空栈
void Clear_SeqStack(SeqStack* stack) {
    if(stack == NULL) return;

    for(int i = 0;i < stack->size;i++) {
        stack->data[i] = NULL;
    }
    stack->size = 0;
}